#include <iostream>
#include <map>
#include <string>
#include <utility>

using std::cout;
using std::endl;
using std::multimap;
using std::string;
using std::pair;
using std::make_pair;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem :  con)
    {
        cout << elem.first << "  " << elem.second << endl;
    }
}

void test()
{
    //multimap的基本特征
    //1、存放key-value类型，key值是不唯一的，可以重复，value值
    //既可以重复，也可以不重复
    //2、默认情况下，会按照key值升序排列
    //3、底层采用红黑树
    multimap<int, string> number = {
        {1, "hello"},
        {3, "world"},
        {6, "beijing"},
        pair<int, string>(2, "hello"),
        pair<int, string>(4, "wuhan"),
        make_pair(7, "wangdao"),
        make_pair(3, "wangdao"),
    };
    display(number);

    cout << endl << "multimap的查找" << endl;
    size_t cnt = number.count(4);
    cout << "cnt = " << cnt << endl;

    auto it = number.find(7);
    if(it != number.end())
    {
        cout << "该元素存在multimap中 " 
             << it->first <<"  " << it->second << endl;
    }
    else
    {
        cout << "该元素不存在multimap中" << endl;
    }

    cout << endl << "multimap的insert操作" << endl;
    auto ret = number.insert({8, "dongjing"});
    display(number);

    cout << endl << "multimap的删除" << endl;
    it = number.end();
    --it;
    cout << it->first << "  " << it->second << endl;
    number.erase(it);
    number.erase(3);
    display(number);

#if 0
    cout << endl << "multimap的下标操作" << endl;
    cout << "number[2] = " << number[2] << endl;//查找
    cout << "number[5] = " << number[5] << endl;//插入
    display(number);

    cout << endl << endl;
    number[5] = "beijing";//修改
    display(number);
#endif
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

